﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PlanningGIS.Util.Net;

namespace PlanningGIS.Util.Convertor
{
    /// <summary>
    /// 坐标转换器
    /// </summary>
    public static class CoordinateConvertor
    {
        /// <summary>
        /// 根据百度坐标获取WGS坐标(利用http://www.zdoz.net/提供的转换接口)
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns>返回格式为"[x];[y]" 若转换失败，则返回的字符串为"error:[错误提示]"</returns>
        public static string Baidu2WGS84(double x,double y)
        {
            try
            {
                string wholeUrl = "http://api.zdoz.net/bd2wgs.aspx?lat=" + y.ToString() + "&lng=" + x.ToString();

                HttpRequestHelper http = new HttpRequestHelper();
                byte[] result = http.GetGetResponse(wholeUrl);
                string rst = System.Text.Encoding.UTF8.GetString(result);
                rst = rst.TrimEnd('}');
                string[] coords = rst.Split(',');
                double xx = Double.Parse(coords[0].Split(':')[1]);
                double yy = Double.Parse(coords[1].Split(':')[1]);

                return xx.ToString() + ";" + yy.ToString();
            }
            catch (Exception ex)
            {
                return "error:"+ex.Message;
            }
        }
    }
}
